package com.yonyou.ucf.mdf.app.isvdxq.secondpart.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.ucf.mdf.app.isvdxq.ReturnResult.DataResult;
import com.yonyou.ucf.mdf.app.isvdxq.common.DxqConfig;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.entity.PurOut;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.entity.RfidCodebind;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.RFIDCodeDetail.entity.storeRecordOtherOut;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.location.service.OrderNoticeService;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.entity.Checkdiff;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.mapper.CheckdiffMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.mapper.CheckstockMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.model.OtherInAndOut.CheckResultOtherInAndOutDto;
import com.yonyou.ucf.mdf.app.isvdxq.firstpart.pandian.model.checkstockMainOrder;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.entity.*;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.mapper.impl.EpclistMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.mapper.impl.StorerecordEntityMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.mapper.impl.TaglistMapperImpl;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.model.OtherInRecordBean;
import com.yonyou.ucf.mdf.app.isvdxq.secondpart.model.OtherInReturnBean;
import com.yonyou.ucf.mdf.app.isvdxq.utils.MyTool;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class StorerecordService {


    @Autowired
    private StorerecordEntityMapperImpl storerecordEntityMapper;
    @Autowired
    private EpclistMapperImpl epclistMapper;

    @Autowired
    CheckdiffMapperImpl tempCheckdiffMapper;

    @Autowired
    TaglistMapperImpl tempTaglistMapper;

    @Autowired
    CheckstockMapperImpl tempCheckstockMapper;


    public StorerecordEntity SelectById(String id) {
        return storerecordEntityMapper.selectByPrimaryKey(id);
    }

    public List<StorerecordEntity> SelectByOrderid(String orderid, String tenant_id, int epcstatus) {
        return storerecordEntityMapper.selectListByOrderid(orderid, tenant_id, String.valueOf(epcstatus));
    }

    public List<StorerecordEntity> SelectEntityList(String orderid, String ordertype, String tenant_id) {
        return storerecordEntityMapper.selectEntityList(orderid, ordertype, tenant_id);
    }

    public Integer EditEntity(List<StorerecordEntity> updateList, String tenant_id) throws Exception {
        return storerecordEntityMapper.EditEntity(updateList, tenant_id);
    }

    public Integer EditStatus(List<String> strList, String tenant_id, String modifier, String epc_status, String allot_status) {
        return storerecordEntityMapper.EditStatus(strList, tenant_id, modifier, epc_status, allot_status);
    }

    public List<StorerecordEntity> getDaohuoList(String orderid, String tenant_id, String userId) throws Exception {
        return storerecordEntityMapper.getDaohuoList(orderid, tenant_id, userId);
    }

    //新增并更新
    public Integer InsertBatch(List<StorerecordEntity> list) {
        if (list.size() == 0) return 0;
        long start = System.currentTimeMillis();
        int res = storerecordEntityMapper.insertBatch(list);
        long end = System.currentTimeMillis();
        System.out.println("---------------" + (start - end) + "---------------");
        return res;
    }

    public int deleteStorerecord(List<String> strList, String ordertype, String modifier, String tenant_id) {
        return storerecordEntityMapper.deleteStorerecord(strList, ordertype, modifier, tenant_id);
    }

    public List<StorerecordEntity> selectStorerecordByEpcIds(List<String> strList, String ordertype, String tenantId) {
        return storerecordEntityMapper.selectListByEpcIds(strList, ordertype, tenantId);
    }

    public List<StorerecordEntity> selectStorerecordByEpcs(List<String> strList, Integer ordertype, Integer epcstatus, String tenantId) {
        return storerecordEntityMapper.selectListByEpcs(strList, String.valueOf(ordertype), String.valueOf(epcstatus), tenantId);
    }

    // 拣货 记录信息写入
    public static StorerecordEntity MakeEntityForPick(PurOut CodeBind, String batchNum, EpcEntity tempEntity,
                                                      String pickStatus) {
        String epcid = CodeBind.getRfidcodeId();
        String tenantId = CodeBind.getTenant_id();
        String modifier = CodeBind.getModifier();

        StorerecordEntity storerecordEntity = new StorerecordEntity();
        // 单据绑定信息
        storerecordEntity.setOrderid(CodeBind.getOrderid());
        storerecordEntity.setOrdercode(CodeBind.getOrdercode());//上游订单号
        storerecordEntity.setOrderdetailid(CodeBind.getOrderdetailid());
        storerecordEntity.setOrderstatus(Integer.parseInt(CodeBind.getOrderstatus())); //上游单据状态
        storerecordEntity.setOrdertype(CodeBind.getOrdertype()); //单据类型
        //基础信息
        storerecordEntity.setPickbatchno(batchNum);
        storerecordEntity.setPickstatus(DxqConfig.StatusOk);
        storerecordEntity.setId(MyTool.getRandomId());
        storerecordEntity.setYtenant_id(tenantId);
        storerecordEntity.setRfidcodeId(epcid);
        storerecordEntity.setRfidcode(tempEntity.getRfid_code());
        storerecordEntity.setEpc_tid(tempEntity.getEpc_tid());
        storerecordEntity.setF_stock_count(tempEntity.getF_stock_count());
        storerecordEntity.setCreate_time(MyTool.GenerateTime());
        storerecordEntity.setCreator(modifier);
        storerecordEntity.setModify_time(MyTool.GenerateTime());
        storerecordEntity.setModifier(modifier);
        storerecordEntity.setBatchno(tempEntity.getBatchno());
        //仓库信息
        storerecordEntity.setStockId(tempEntity.getWarehouse_id());
        storerecordEntity.setWarehouse_id(tempEntity.getWarehouse_id());
        storerecordEntity.setWarehouse_code(tempEntity.getWarehouse_code());
        storerecordEntity.setWarehouse_name("");
        storerecordEntity.setLocationId(tempEntity.getLocation_id());
        //物料信息、计量单位
        storerecordEntity.setProduct_id(tempEntity.getProduct_id());
        storerecordEntity.setProduct_name(tempEntity.getProduct_name());
        storerecordEntity.setProduct_sku_id(tempEntity.getProduct_sku_id());
        storerecordEntity.setProduct_sku_name(tempEntity.getProduct_sku_name());
        storerecordEntity.setUnitId(tempEntity.getUnitId());
        storerecordEntity.setUnit_name(tempEntity.getUnit_name());
        //库存单位 入库时需要回写
        storerecordEntity.setStockUnitId(tempEntity.getStockUnitId());
        storerecordEntity.setStock_unit_id(tempEntity.getStockUnitId());
        storerecordEntity.setStock_unit_code("");
        storerecordEntity.setStock_unit_name(tempEntity.getStock_unit_name());

        //拣货回写
        storerecordEntity.setEpc_status(Integer.parseInt(pickStatus));
        storerecordEntity.setLocationId(tempEntity.getLocation_id());
        //记录计价信息 用于出库提交
        storerecordEntity.setPriceUOM(CodeBind.getPriceUOM());
        storerecordEntity.setPriceUOM(CodeBind.getPriceUOM());
        storerecordEntity.setPriceUOM(CodeBind.getInvExchRate());
        storerecordEntity.setPriceUOM(CodeBind.getUnitExchangeType());
        storerecordEntity.setPriceUOM(CodeBind.getInvPriceExchRate());
        storerecordEntity.setPriceUOM(CodeBind.getUnitExchangeTypePrice());

        if (!StringUtils.isEmpty(tempEntity.getProductDate()))
            storerecordEntity.setProductDate(tempEntity.getProductDate());
        if (!StringUtils.isEmpty(tempEntity.getInvalidDate()))
            storerecordEntity.setInvalidDate(tempEntity.getInvalidDate());
        //回写出入库单据信息 私有方案
//      storerecordEntity.setPoid(CodeBind.getPoid());
//      storerecordEntity.setPodetailid(CodeBind.getPodetailid());
//      storerecordEntity.setPocode(CodeBind.getPocode());

        return storerecordEntity;
    }

    // 发签入库 记录信息写入
    public static StorerecordEntity MakeEntityForPad(RfidCodebind CodeBind, String batchNum, String epcid) {
        String tenantId = CodeBind.getTenant_id();
        String modifier = CodeBind.getModifier();

        //二期方案 补录信息修改
        StorerecordEntity storerecordEntity = new StorerecordEntity();
        storerecordEntity.setType(0); //Pad默认
        storerecordEntity.setOrderid(CodeBind.getOrderid());
        storerecordEntity.setOrderdetailid(CodeBind.getOrderdetailid());
        storerecordEntity.setOrdercode(CodeBind.getOrdercode());//上游订单号
        storerecordEntity.setOrderstatus(CodeBind.getOrderstatus()); //上游单据状态
        storerecordEntity.setOrdertype(CodeBind.getOrdertype()); //单据类型
        //基础信息
        storerecordEntity.setPickbatchno(batchNum);
        storerecordEntity.setPickstatus(DxqConfig.StatusOk);
        storerecordEntity.setId(MyTool.getRandomId());
        storerecordEntity.setYtenant_id(tenantId);
        storerecordEntity.setRfidcodeId(epcid);
        storerecordEntity.setRfidcode(CodeBind.getRfid_code());
        storerecordEntity.setEpc_tid("");
        storerecordEntity.setEpc_status(CodeBind.getRfid_status());
        storerecordEntity.setF_stock_count(CodeBind.getF_stock_count());
        storerecordEntity.setCreate_time(MyTool.GenerateTime());
        storerecordEntity.setCreator(modifier);
        storerecordEntity.setModify_time(MyTool.GenerateTime());
        storerecordEntity.setModifier(modifier);
        storerecordEntity.setBatchno(CodeBind.getBatchno());
        //仓库信息
        storerecordEntity.setStockId(CodeBind.getWarehouse_id());
        storerecordEntity.setWarehouse_id(CodeBind.getWarehouse_id());
        storerecordEntity.setWarehouse_code(CodeBind.getWarehouse_code());
        storerecordEntity.setWarehouse_name("");
        storerecordEntity.setLocationId(CodeBind.getLocationId());
        //方案处理
        storerecordEntity.setProduct_id(CodeBind.getProduct_id());
        storerecordEntity.setProduct_code("");
        storerecordEntity.setProduct_name(CodeBind.getProduct_name());
        storerecordEntity.setProduct_sku_id(CodeBind.getProduct_sku_id());
        storerecordEntity.setProduct_sku_code("");
        storerecordEntity.setProduct_sku_name(CodeBind.getProduct_sku_name());
        storerecordEntity.setUnitId(CodeBind.getUnitId());
        storerecordEntity.setUnit_name(CodeBind.getUnit_name());
        //回写 库存单位
        storerecordEntity.setStockUnitId(CodeBind.getStockUnitId());
        storerecordEntity.setStock_unit_id(CodeBind.getStockUnitId());
        storerecordEntity.setStock_unit_name(CodeBind.getStock_unit_name());
        storerecordEntity.setStock_unit_code("");

        if (!StringUtils.isEmpty(CodeBind.getProductDate()))
            storerecordEntity.setProductDate(CodeBind.getProductDate());
        if (!StringUtils.isEmpty(CodeBind.getInvalidDate()))
            storerecordEntity.setInvalidDate(CodeBind.getInvalidDate());
        //入库单信息
        storerecordEntity.setPoid("");
        storerecordEntity.setPocode("");
        storerecordEntity.setPodetailid("");
        //计价信息
        storerecordEntity.setPriceUOM("");
        storerecordEntity.setPriceUOM("");
        storerecordEntity.setPriceUOM("");
        storerecordEntity.setPriceUOM("");
        storerecordEntity.setPriceUOM("");
        storerecordEntity.setPriceUOM("");

        return storerecordEntity;
    }

    public static StorerecordEntity MakeEntityForPC(PrelableEntity item, EpcEntity tempEntity, String batchNum
            , String tenantId, String modifier, int sourcetype) {
        //记录
        StorerecordEntity storerecordEntity = new StorerecordEntity();
        storerecordEntity.setOrderid(item.getOrderid());
        storerecordEntity.setOrderdetailid(item.getOrderdetailid());
        storerecordEntity.setOrdercode(item.getOrdercode());//上游订单号
        storerecordEntity.setOrderstatus(item.getStatus()); //上游单据状态
        storerecordEntity.setOrdertype(item.getOrdertype()); //单据类型
        //基础信息
        storerecordEntity.setType(sourcetype);//用于区分PC/pad
        storerecordEntity.setPickbatchno(batchNum);
        storerecordEntity.setPickstatus(DxqConfig.StatusOk);
        storerecordEntity.setId(MyTool.getRandomId());
        storerecordEntity.setYtenant_id(tenantId);
        storerecordEntity.setRfidcodeId(tempEntity.getId());
        storerecordEntity.setRfidcode(tempEntity.getRfid_code());
        storerecordEntity.setEpc_tid("");
        storerecordEntity.setF_stock_count(tempEntity.getF_stock_count());
        storerecordEntity.setCreate_time(MyTool.GenerateTime());
        storerecordEntity.setCreator(modifier);
        storerecordEntity.setModify_time(MyTool.GenerateTime());
        storerecordEntity.setModifier(modifier);
        storerecordEntity.setBatchno(tempEntity.getBatchno());

        //方案处理
        storerecordEntity.setStockId(item.getWarehouseId());
        storerecordEntity.setWarehouse_id(item.getWarehouseId());
        storerecordEntity.setWarehouse_code(item.getWarehousecode());
        storerecordEntity.setWarehouse_name(item.getWarehousename());
        //物料信息、计量单位
        storerecordEntity.setProduct_id(item.getProductId());
        storerecordEntity.setProduct_code(item.getProductcode());
        storerecordEntity.setProduct_name(item.getProductname());
        storerecordEntity.setProduct_sku_id(item.getProductskuId());
        storerecordEntity.setProduct_sku_code(item.getProductskucode());
        storerecordEntity.setProduct_sku_name(item.getProductskuname());
        storerecordEntity.setUnitId(item.getUnitId());
        storerecordEntity.setUnit_code(item.getUnitcode());
        storerecordEntity.setUnit_name(item.getUnitname());
        //回写 库存单位
        storerecordEntity.setStockUnitId(tempEntity.getStockUnitId());
        storerecordEntity.setStock_unit_id(tempEntity.getStockUnitId());
        storerecordEntity.setStock_unit_code(item.getPurUOM_Code());
        storerecordEntity.setStock_unit_name(tempEntity.getStock_unit_name());

        if (!StringUtils.isEmpty(tempEntity.getProductDate()))
            storerecordEntity.setProductDate(tempEntity.getProductDate());
        if (!StringUtils.isEmpty(tempEntity.getInvalidDate()))
            storerecordEntity.setInvalidDate(tempEntity.getInvalidDate());
        //入库单信息
        storerecordEntity.setPoid("");
        storerecordEntity.setPocode("");
        storerecordEntity.setPodetailid("");
        //计价信息
        storerecordEntity.setPriceUOM("");
        storerecordEntity.setPriceUOM("");
        storerecordEntity.setPriceUOM("");
        storerecordEntity.setPriceUOM("");
        storerecordEntity.setPriceUOM("");
        storerecordEntity.setPriceUOM("");

        return storerecordEntity;
    }

    public StorerecordEntity SelectByEpcIds(List<String> ids_recordList, String tenantid) throws Exception {
        return storerecordEntityMapper.checkEntityByEpcIds(ids_recordList, tenantid);
    }

    public StorerecordEntity SelectEntityByPoid(String poid, String tenant_id) {
        return storerecordEntityMapper.selectEntityByPoid(poid, tenant_id);
    }

    public List<StorerecordEntity> selectStorerecordByIds(List<String> ids_record, String tenantid) {
        return storerecordEntityMapper.selectStorerecordByIds(ids_record, tenantid);
    }

    public List<StorerecordEntity> SelectAll(String tenantId) {
        return storerecordEntityMapper.selectAll(tenantId);
    }

    public List<StorerecordEntity> SelectPage(String tenantId, int m, int n) {
        return storerecordEntityMapper.selectPage(tenantId, m, n);
    }

    public List<CheckresultEntity> SelectAllForCheckresult(String tenantId) {
        return storerecordEntityMapper.selectAllForCheckresult(tenantId);
    }

    public List<CheckdiffEntity> SelectAllForCheckdiff(String tenantId) {
        return storerecordEntityMapper.selectAllForCheckdiff(tenantId);
    }

    @Autowired
    OrderNoticeService orderNoticeService;

    /**
     * 其他入库 回写更新相关记录
     * 用友入库回写
     *
     * @param dataList
     * @param tenantId
     * @param ordertype
     * @param strCallback
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public DataResult UpdateForCallback(List<CheckResultOtherInAndOutDto> dataList, String tenantId, String ordertype, String strCallback, String batchnoCallback) throws Exception {
        DataResult dataResult = new DataResult(0, "操作失败");
        if (dataList == null || dataList.size() == 0) {
            dataResult = new DataResult(0, "提交的数据条数为空!");
            return dataResult;
        }

        try {
            //生成批次号
            String picicode = MyTool.GenerateBatchnoDXQ(ordertype);
            List<String> epcList = dataList.stream().map(it -> it.getEpccode()).distinct().collect(Collectors.toList());
            OtherInReturnBean otherInReturnBean = JSONObject.parseObject(strCallback, OtherInReturnBean.class);
            if (otherInReturnBean.getOthInRecords().size() == 0) {
                dataResult = new DataResult(0, "回写数据条数为空!");
                return dataResult;
            }
            String poid = otherInReturnBean.getId(); //入库单主单号
            String pocode = otherInReturnBean.getCode();
            List<OtherInRecordBean> othInRecords = otherInReturnBean.getOthInRecords().stream()
                    .sorted(Comparator.comparing(OtherInRecordBean::getRowno))
                    .collect(Collectors.toList());
            List<EpcEntity> epcEntityList = epclistMapper.selectByEpcs(epcList, tenantId);
            List<String> epcIds = epcEntityList.stream().map(it -> it.getId()).distinct().collect(Collectors.toList());
            List<StorerecordEntity> storerecordEntities = storerecordEntityMapper.selectListByEpcIds(epcIds, ordertype, tenantId);
            if (epcEntityList.size() == 0 || storerecordEntities.size() == 0) {
                dataResult = new DataResult(0, "本地数据异常");
                return dataResult;
            }

            int i = 0;
            for (EpcEntity item : epcEntityList) {
                OtherInRecordBean recordrow = othInRecords.get(i);
                String invaliddate = recordrow.getInvaliddate();
                item.setRfid_status(1);
                item.setInvalidDate(invaliddate);
                item.setBatch_no(batchnoCallback);
                String remarkString = StringUtils.isEmpty(item.getRemark()) ? "" : item.getRemark();
                item.setRemark(String.format("%s|%s", "盘点补签", remarkString));
                i++;
            }
            i = 0;
            for (StorerecordEntity item : storerecordEntities) {
                EpcEntity entity = epcEntityList.stream().filter(it -> it.getId().equals(item.getRfidcodeId())).findFirst().orElse(null);
                if (entity != null) {
                    OtherInRecordBean recordrow = othInRecords.get(i);
                    String podetailid = recordrow.getId();
                    String invaliddate = !StringUtils.isEmpty(recordrow.getInvaliddate()) ? recordrow.getInvaliddate() : entity.getInvalidDate();
                    item.setEpc_status(entity.getRfid_status());
                    item.setEpc_tid(entity.getEpc_tid());
                    item.setLocationId(entity.getLocation_id());
                    item.setBatch_no(batchnoCallback);
                    item.setPoid(poid);
                    item.setPocode(pocode);
                    item.setPodetailid(podetailid);
                    item.setInvalidDate(invaliddate);
                    i++;
                }
            }

            if (dataList.size() > 0 && storerecordEntities.size() > 0) {
                epclistMapper.insertBatch(epcEntityList);
                storerecordEntityMapper.insertBatch(storerecordEntities);
            }
            // 更新差异补签入库部分数据状态 已处理=2
            List<String> strList = dataList.stream().map(it -> it.getCheckDiffId()).distinct().collect(Collectors.toList());
            if (strList.size() > 0) {
                tempCheckdiffMapper.UpdateCheckDiffListStatus(strList, "2", tenantId);
            }


            dataResult = new DataResult(1, "信息保存成功");
            dataResult.setData(batchnoCallback);
        } catch (Exception ex) {
            dataResult.setMessage(ex.getMessage());
        }
        return dataResult;
    }

    /**
     * 用友其他出库回写
     *
     * @param dataList
     * @param tenantid
     * @param ordertype
     * @param dataDetail
     * @param batch_no
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public DataResult UpdateForOutCallback(List<CheckResultOtherInAndOutDto> dataList, String tenantid, String ordertype, String dataDetail, String batch_no) {
        DataResult dataResult = new DataResult(0, "操作失败");
        if (dataList == null || dataList.size() == 0) {
            dataResult = new DataResult(0, "提交的数据条数为空!");
            return dataResult;
        }
        List<String> CheckDiffIds = dataList.stream().filter(it -> StringUtils.isNotEmpty(it.getCheckDiffId()))
                .map(it -> it.getCheckDiffId()).distinct().collect(Collectors.toList());
        if (CheckDiffIds.size() == 0) {
            dataResult = new DataResult(0, "提交的数据条数为空!");
            return dataResult;
        }

        try {
            //生成批次号
            String picicode = MyTool.GenerateBatchnoDXQ(ordertype);
            JSONObject jsonData = JSONObject.parseObject(dataDetail);
            JSONArray jsonArray = jsonData.getJSONArray("othOutRecords");
            // 回写对应单据的状态 删除标签 和托盘绑定关系，回写单据
            List<Checkdiff> listcheckdiffs = tempCheckdiffMapper.selectCheckdiffByIds(tenantid, CheckDiffIds);
            List<String> RfidsIds = listcheckdiffs.stream().map(it -> it.getCoderfid()).distinct().collect(Collectors.toList());
            List<EpcEntity> listEpc = new ArrayList<>();
            if (RfidsIds.size() > 0)
                listEpc = epclistMapper.selectByIds(RfidsIds, tenantid);
            if (listEpc.size() == 0) {
                dataResult = new DataResult(0, "关联物资数据不存在!");
                return dataResult;
            }
            // 盘点单ID
            String checkId = listcheckdiffs.get(0).getCheckID();
            checkstockMainOrder checkstock = tempCheckstockMapper.GetCheckStockById(checkId, tenantid);
            if (checkstock == null) {
                dataResult = new DataResult(0, "盘点单不存在");
                return dataResult;
            }
            List<storeRecordOtherOut> storeRecordOtherOutList = new ArrayList<>();
            for (int i = 0; i < jsonArray.size(); i++) {
                JSONObject obj = jsonArray.getJSONObject(i);
                storeRecordOtherOut srooObj = new storeRecordOtherOut();
                String idrecord = MyTool.getRandomId();
                List<String> memoList = new ArrayList<>();
                if (StringUtils.isNotEmpty(obj.getString("memo")))
                    memoList = Arrays.asList(obj.getString("memo").split("\\|"));
                String checkDiffId = memoList.get(1); //checkdiffId
                Checkdiff checkdiffEntity = listcheckdiffs.stream().filter(checkdiff -> checkDiffId.equals(checkdiff.getId())).findFirst().orElse(null);
                EpcEntity epcEntity = listEpc.stream().filter(coderfid -> checkDiffId.equals(coderfid.getId())).findFirst().orElse(null);
                if (checkdiffEntity == null || epcEntity == null) continue;
                srooObj.setRfidcode(epcEntity.getRfid_code());
                srooObj.setRfidcodeId(checkdiffEntity.getCoderfid());
                srooObj.setId(idrecord);
                srooObj.setPoid(jsonData.getString("id"));
                srooObj.setPocode(jsonData.getString("code"));
                srooObj.setPodetailid(obj.getString("id"));

                srooObj.setOrdercode(checkstock.getCheckNo());
                srooObj.setOrderid(checkdiffEntity.getCheckID());
                srooObj.setSourceid(checkdiffEntity.getId());

                srooObj.setOrdertype(ordertype);
                srooObj.setOrderstatus(1);
                srooObj.setOrderdetailid(obj.getString("id"));
                srooObj.setStockId(jsonData.getString("warehouse"));
                srooObj.setStockUnitId(obj.getString("stockUnitId"));

                srooObj.setSource("盘点差异表");
                srooObj.setUnit_name(obj.getString("product_unitName"));
                srooObj.setWarehouse_id(jsonData.getString("warehouse"));
                srooObj.setWarehouse_name(jsonData.getString("warehouse_name"));
                srooObj.setProduct_code(obj.getString("product_cCode"));
                srooObj.setProduct_name(obj.getString("product_cName"));
                srooObj.setProduct_id(obj.getString("product"));
                srooObj.setProduct_sku_id(obj.getString("productsku"));
                srooObj.setF_stock_count(obj.getDoubleValue("qty"));
                srooObj.setBatch_no(batch_no);
                if (obj.getString("batchno") == null || obj.getString("batchno").length() == 0) {
                    srooObj.setBatchno(null);
                } else {
                    srooObj.setBatchno(obj.getString("batchno"));
                }
                if (obj.getString("invaliddate") == null || obj.getString("invaliddate").length() == 0) {
                    srooObj.setInvalidDate(null);
                } else {
                    srooObj.setInvalidDate(obj.getString("invaliddate"));
                }
                if (obj.getString("producedate") == null || obj.getString("producedate").length() == 0) {
                    srooObj.setProductDate(null);
                } else {
                    srooObj.setProductDate(obj.getString("producedate"));
                }
                srooObj.setCreator(obj.getString("creator"));
                srooObj.setModifier(obj.getString("creator"));
                srooObj.setPubts(MyTool.GenerateTime("yyyy-MM-dd HH:mm:ss"));
                srooObj.setTenant_id(tenantid);
                storeRecordOtherOutList.add(srooObj);
            }

            // 写单据
            if (storeRecordOtherOutList.size() > 0) {
                int insertStoreCord = storerecordEntityMapper.InsertStorerecordOtherOut(storeRecordOtherOutList);
            }
            if (listEpc.size() > 0) {
                for (EpcEntity item : listEpc) {
                    item.setPk_temp("outbackother");
                }
                epclistMapper.insertBatchHistory(listEpc);
                // 删除coderfid
                int deleteEpc = epclistMapper.deleteByIds(RfidsIds, tenantid);
                // 删除关联关系
                List<String> epcs = listEpc.stream().map(it -> it.getRfid_code()).distinct().collect(Collectors.toList());
                int deleteTagInfo = tempTaglistMapper.deleteByEpcKeys(epcs, tenantid);
            }
            int returnCount = tempCheckdiffMapper.UpdateCheckDiffListStatus(CheckDiffIds, "1", tenantid);

            dataResult.setStatus(1);
            dataResult.setMessage("操作成功");
        } catch (Exception ex) {
            dataResult.setMessage(ex.getMessage());
        }
        return dataResult;
    }

    public Integer EditBatchno_dxq(List<String> epcs, String orderid, String ordertype, String tenantId, String batchno_dxq) {
        return storerecordEntityMapper.EditBatchno_dxq(epcs, orderid, ordertype, tenantId, batchno_dxq);
    }

    public List<StorerecordEntity> SelectEntityByCodes(List<String> codes, String tenantId) {
        return storerecordEntityMapper.selectEntityByCodes(codes, tenantId);
    }
}
